/**
 * @file TeacherCourseClasses.vue
 * @description 教师课程班级页面，显示特定教师教授的特定课程的班级列表
 * @created 2024-07-10
 */

<template>
  <div class="course-classes-container">
    <!-- 返回按钮 -->
    <div class="back-btn" @click="router.back()">
      <span class="icon">←</span> 返回教师详情
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <div class="loading-text">加载班级数据中...</div>
    </div>

    <div v-else class="content">
      <!-- 课程信息卡片 -->
      <div class="course-info-card">
        <div class="course-header">
          <div class="course-icon">📚</div>
          <div class="course-basic-info">
            <h2>{{ courseInfo.name }}</h2>
            <div class="info-tags">
              <span class="tag">{{ `${courseInfo.credit}学分` }}</span>
              <span class="tag">{{ `${courseInfo.hours}学时` }}</span>
              <span class="tag">{{ teacherInfo.department_name }}</span>
            </div>
          </div>
          <div class="teacher-info">
            <div class="teacher-name">
              <span>授课教师: </span>
              <strong>{{ teacherInfo.name }}</strong>
            </div>
            <div class="teacher-title">{{ teacherInfo.title }}</div>
          </div>
        </div>
        
        <div class="course-description">
          <h3>课程描述</h3>
          <p>{{ courseInfo.description || '暂无课程描述' }}</p>
        </div>
      </div>

      <!-- 班级列表 -->
      <div class="classes-card">
        <div class="card-header">
          <h2>班级列表</h2>
          <button class="add-btn" @click="addNewClass">+ 添加班级</button>
        </div>

        <div v-if="!classes || classes.length === 0" class="empty-data">
          暂无班级数据
        </div>
        <div v-else class="classes-list">
          <div v-for="classItem in classes" :key="classItem.id" class="class-card">
            <div class="class-header">
              <h3>{{ classItem.name }}</h3>
              <div class="class-tags">
                <span class="tag">{{ classItem.grade_year }}级</span>
                <span class="tag">{{ classItem.student_count }}名学生</span>
              </div>
            </div>
            <div class="class-actions">
              <button 
                class="view-btn"
                @click="viewClassGrades(classItem)"
              >
                查看成绩
              </button>
              <button 
                class="edit-btn"
                @click="editClass(classItem)"
              >
                编辑
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getTeacherCourseClasses } from '@/api/admin/teacher'

const route = useRoute()
const router = useRouter()
const teacherId = route.params.teacherId
const courseId = route.params.courseId

// 状态
const loading = ref(true)

// 数据
const teacherInfo = ref({})
const courseInfo = ref({})
const classes = ref([])

// 获取教师课程班级数据
const fetchCourseClasses = async () => {
  loading.value = true
  try {
    const res = await getTeacherCourseClasses(teacherId, courseId)
    if (res.status === 'success') {
      teacherInfo.value = res.data.teacher
      courseInfo.value = res.data.course
      classes.value = res.data.classes
    } else {
      console.error('获取班级列表失败:', res.message)
    }
  } catch (error) {
    console.error('获取班级列表出错:', error)
  } finally {
    loading.value = false
  }
}

// 添加新班级
const addNewClass = () => {
  // TODO: 实现添加班级功能
  alert('添加班级功能尚未实现')
}

// 编辑班级
const editClass = (classItem) => {
  // TODO: 实现编辑班级功能
  alert(`编辑班级 ${classItem.name} 功能尚未实现`)
}

// 查看班级学生成绩
const viewClassGrades = (classItem) => {
  router.push(`/admin/teacher/${teacherId}/course/${courseId}/class/${classItem.id}/grades`)
}

onMounted(() => {
  fetchCourseClasses()
})
</script>

<style scoped>
.course-classes-container {
  padding: 30px;
  background: #f5f6fa;
  min-height: 100vh;
}

.back-btn {
  display: inline-flex;
  align-items: center;
  margin-bottom: 20px;
  padding: 8px 16px;
  background: white;
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s;
}

.back-btn:hover {
  background: #f0f0f0;
}

.back-btn .icon {
  margin-right: 8px;
}

.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100px 0;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 20px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-text {
  color: #666;
  font-size: 16px;
}

.content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.course-info-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.course-header {
  display: flex;
  padding: 20px;
  background: #f0f5ff;
  border-bottom: 1px solid #e6e6e6;
}

.course-icon {
  width: 60px;
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #e6f7ff;
  border-radius: 50%;
  font-size: 30px;
  margin-right: 20px;
}

.course-basic-info {
  flex-grow: 1;
}

.course-basic-info h2 {
  margin: 0;
  margin-bottom: 10px;
  font-size: 24px;
  color: #1a1a1a;
}

.info-tags {
  display: flex;
  gap: 10px;
}

.tag {
  padding: 4px 8px;
  background: #e6f7ff;
  color: #1890ff;
  border-radius: 4px;
  font-size: 14px;
}

.teacher-info {
  text-align: right;
  min-width: 150px;
}

.teacher-name {
  font-size: 16px;
  margin-bottom: 5px;
}

.teacher-title {
  font-size: 14px;
  color: #666;
}

.course-description {
  padding: 20px;
}

.course-description h3 {
  margin: 0;
  margin-bottom: 15px;
  font-size: 18px;
  color: #1a1a1a;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 10px;
}

.course-description p {
  margin: 0;
  line-height: 1.6;
  color: #333;
}

.classes-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  background: #f0f5ff;
  border-bottom: 1px solid #e6e6e6;
}

.card-header h2 {
  margin: 0;
  font-size: 20px;
  color: #1a1a1a;
}

.add-btn {
  padding: 8px 16px;
  background: #1890ff;
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.add-btn:hover {
  background: #40a9ff;
}

.empty-data {
  text-align: center;
  padding: 40px 0;
  color: #999;
  font-size: 16px;
}

.classes-list {
  padding: 20px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.class-card {
  border: 1px solid #f0f0f0;
  border-radius: 8px;
  overflow: hidden;
}

.class-header {
  padding: 15px;
  background: #f9f9f9;
  border-bottom: 1px solid #f0f0f0;
}

.class-header h3 {
  margin: 0;
  margin-bottom: 10px;
  font-size: 16px;
  color: #1a1a1a;
}

.class-tags {
  display: flex;
  gap: 8px;
}

.class-actions {
  padding: 15px;
  display: flex;
  justify-content: space-between;
}

.view-btn, .edit-btn {
  padding: 6px 12px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.view-btn {
  background: #1890ff;
  color: white;
}

.edit-btn {
  background: #fff;
  color: #1890ff;
  border: 1px solid #1890ff;
}

.view-btn:hover {
  background: #40a9ff;
}

.edit-btn:hover {
  background: #e6f7ff;
}
</style> 